package Thread;

import java.util.concurrent.CountDownLatch;

/**
 * Created with IntelliJ IDEA.
 * Description: CountDownLatch的练习
 */
public class ThreadDemo38 {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(10);
        for (int i = 0; i < 10; i++) {
            Thread t = new Thread(() -> {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName() + "执行结束");
                //这步操作才是告诉latch，一个线程结束
                latch.countDown();
            });
            t.start();
        }
        //latch中存在类似一个计数器的属性，一个信号发过来，计数器值就减 1
        //如果数目没到之前参数的个数，那么就阻塞等待
        latch.await();
        System.out.println("全部执行完毕");
    }

}
